package com.manlitech.cloudboot.baseboot.controller;
/*
 *Created by Intellij IDEA.
 *User:  Shensg
 *Date:  2021/3/1
 */

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manlitech.cloudboot.baseboot.mapper.Test;
import com.manlitech.cloudboot.baseboot.mapper.TestMapper;
import com.manlitech.cloudboot.baseboot.service.TestServiceImpl;
import com.manlitech.cloudboot.basebootconfig.annotation.Permissions;
import com.manlitech.cloudboot.common.enums.ResultEnums;
import com.manlitech.cloudboot.basebootconfig.exception.MyException;
import com.manlitech.cloudboot.common.utils.Result;
import com.manlitech.cloudboot.common.utils.ResultUtil;
import com.manlitech.cloudboot.config.nacosconfig.bean.Aloha;
import com.manlitech.cloudboot.interfacestore.dto.TestDto;
import com.manlitech.cloudboot.interfacestore.openfeign.EchoService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@GlobalTransactional(rollbackFor = Exception.class)
public class TestController {
    /**
     * 测试
     */
    @Resource
    TestMapper testMapper;

    @Resource
    TestServiceImpl testService;

    @Autowired
    Aloha aloha;

    /*@LoadBalanced
    @Autowired
    public RestTemplate restTemplate;


    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }*/

    /**
     * 测试mapper 和 service 插入
     * @return
     */
    @RequestMapping("/test")
    public Result test(){
        Test test = new Test();
        test.setName(123);
        testMapper.insert(test);

        testService.save(test);
        List<Test> list = testService.lambdaQuery().eq(Test::getName , "123").list();
        return ResultUtil.success(list);
    }

    /**
     * 测试单表lambda查询
     * @return
     */
    @RequestMapping("/test1")
    @SentinelResource(value = "test1")
    public Result test1(@Autowired HttpServletRequest request) throws BlockException {
        log.info("requestId , {}" , request.getHeader("requestId") );
        List<Test> list = testService.lambdaQuery().eq(Test::getName , "123").list();
        return ResultUtil.success(list);
    }

    /**
     * 测试单表lambda查询
     * @return
     */
    @RequestMapping("/test2")
    public Result test2(){
        List<Test> list = testService.lambdaQuery().eq(Test::getName , "123").list();
        return ResultUtil.success(list);
    }

    /**
     * 测试单表lambda 分页
     * @return
     */
    @RequestMapping("/test3")
    public Result test3(){
        //参数一是当前页，参数二是每页个数
        IPage<Test> testPage = new Page<>(1, 2);
        testPage = testService.lambdaQuery().eq(Test::getName , "123").page(testPage);
        return ResultUtil.success(testPage.getRecords());
    }

    /**
     * 测试事务回滚
     * @return
     */
    @RequestMapping("/test4")
    @Transactional(rollbackFor = Exception.class)
    public Result test4(){
        //参数一是当前页，参数二是每页个数
        Test test = new Test();
        test.setName(999);
        testService.save(test);
        if(1 == 1){
            throw new MyException(ResultEnums.SQL_INJECTION);
        }
        return ResultUtil.success(test);
    }

    /**
     * 多数据源
     * @return
     */
    @RequestMapping("/test5")
    @Transactional(rollbackFor = Exception.class)
    @DS("master")
    public Result test5(){
        //参数一是当前页，参数二是每页个数
        Test test = new Test();
        test.setName(999);
        testService.save(test);
        return ResultUtil.success(test);
    }
    @RequestMapping("/test6")
    @Transactional(rollbackFor = Exception.class)
    @DS("slave")
    public Result test6(){
        //参数一是当前页，参数二是每页个数
        Test test = new Test();
        test.setName(999);
        testService.save(test);
        return ResultUtil.success(test);
    }

    @RequestMapping("/testuser")
    public Result testuser(){
        //参数一是当前页，参数二是每页个数
        return ResultUtil.success(aloha);
    }



    /*@GetMapping("/call/echo/{message}")
    public String callEcho(@PathVariable String message) {
        // 访问应用 nacos-discovery-provider-sample 的 REST "/echo/{message}"
        return restTemplate.getForObject("http://nacos-discovery-provider-sample/echo/" + message, String.class);
    }*/

    /**
     * open feign 负载均衡请求
     */

    @Autowired
    private EchoService echoService;

    @GetMapping("/feign/echo/{message}")
    @SentinelResource(value = "feignEcho")
    @DS("master")
    public String feignEcho(@PathVariable String message) {
        log.info("####feignEcho####");
        Test test = new Test();
        test.setName(99900);
        testService.save(test);
        echoService.echo(message);
        if(1 == 1){
            throw new MyException(ResultEnums.SQL_INJECTION);
        }
        return "success" ;
    }

    @GetMapping("/echo/{message}")
    @DS("slave")
    public String echo(@PathVariable String message) {
        log.info("####echo####");
        Test test = new Test();
        test.setName(9990011);
        testService.save(test);
        return "[ECHO] : " + message;
    }

    /**
     * 测试token传递问题
     */
    @RequestMapping("/echo/token")
    public String token(@Autowired  HttpServletRequest request){
        TestDto test = new TestDto();
        test.setName(875);
        log.info("traceId {} " , request.getHeader("traceId"));
        return echoService.ping(test  ,"no head param");
    }

    @RequestMapping("/echo/token/json")
    public String token(@RequestBody TestDto test){
        return echoService.ping(test  ,"no head param");
    }

    @RequestMapping("/ping/token")
    public String ping(@Autowired  HttpServletRequest request  , @RequestBody Test test,  @RequestParam("noHeadToken") String noHeadToken){
        String token = request.getHeader("token");
        log.info("test name , {}" , test.getName());
        log.info("param token , {} " , token);
        log.info("other param , {} " , noHeadToken);
        log.info("traceId {} " , request.getHeader("traceId"));
        return token;
    }

    /**
     * 测试get请求下多参数
     * @param test
     * @return
     */
    @RequestMapping("/echo/token/json/testget")
    public String testget(TestDto test){
        return echoService.pingTestGet(test);
    }

    @RequestMapping("/ping/token/testget")
    @Permissions(module ="xx模块", privilege ="xx模块-修改接口", typeOfAccess = Permissions.METHOD_OPEN)
    public String pingTestGet(@Autowired  HttpServletRequest request  , Test test){
        String token = request.getHeader("token");
        log.info("test id , {}" , test.getId());
        log.info("test name , {}" , test.getName());
        log.info("param token , {} " , token);
        log.info("traceId {} " , request.getHeader("traceId"));
        return token;
    }






}
